package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;


/**
 * @Author 王加鑫
 * @Date 2021/5/4 20:40
 * @Version 1.0
 */
public class TeacherDao {

    private Teacher[] teachers = new Teacher[5];

    public boolean addTeacher(Teacher teacher) {
        //标记位:假设数组已经满了
        int index = -1;
        //遍历数组找到第一个为null的索引值将teacher对象赋值给它
        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i] == null){
                index = i;
                break;
            }
        }
        if(index == -1){
            return false;
        }else {
            teachers[index] = teacher;
            return true;
        }
    }

    /*
    返回老师数组
     */
    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public void deleteTeacher(int index) {
        teachers[index] = null;
    }

    public Teacher selectTeacherByIndex(int index) {
        if(index != -1){
            return teachers[index];
        }
        return null;
    }

    public void updateTeacher(String tId, Teacher teacher) {
        int index = getIndex(tId);
        teachers[index] = teacher;

    }

    public int getIndex(String tId){
        //标记位思想:默认认为数组没有重复sId,flag为false
        int index = -1;
        //指挥dao层查出来所有的数据再进行判断
        Teacher[] teachers = findAllTeacher();
        for (int i = 0; i < teachers.length; i++) {
            //当数组元素不为空且取出来的值等于传过来的sId值时,说明id重复
            if(teachers[i] != null && teachers[i].getId().equals(tId)){
                index = i;
            }
        }
        return index;

    }
}
